package lrscp.lib.utils;

import lrscp.lib.Log;

public class StringUtils {
    public static String removeSuffix(String inString, String suffix) {
        return inString.substring(0, inString.lastIndexOf(suffix));
    }

    public static String getSuffix(String s) {
        if (s.lastIndexOf('.') < 0) return "";
        return s.substring(s.lastIndexOf('.'));
    }

    public static String getDefault(String src, String defaultString) {
        return src == null ? defaultString : src;
    }

    /**
     * replace the unexpected chars in a path string like '\' ':' and so on
     */
    public static String trimPath(String path, char replace) {
        char[] buf = new char[path.length()];
        path.getChars(0, path.length(), buf, 0);
        for (int i = 0; i < buf.length; i++) {
            if ("\t\r\n\\/:\"?*<>|".indexOf(buf[i]) != -1) {
                buf[i] = replace;
            }
        }
        path = new String(buf);
        return path;
    }

    public static String encodeDbString(String in) {
        return in.replaceAll("'", "''");
    }

    public static String decodeDbString(String in) {
        return in.replaceAll("''", "'");
    }

    public static String toString(byte[] buffer) {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (byte b : buffer) {
            sb.append((int) (b & 0xff));
            sb.append(", ");
        }
        sb.append(']');
        sb.deleteCharAt(sb.length() - 2);
        return sb.toString();
    }

    public static void main(String[] args) {
        Log.i(decodeDbString("hell''''o"));
    }
}
